/**************************************************************************************
*
*	Copyright (C) 2011 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	Logic.cpp
*
*	Comments	-	See Logic.h
*
**************************************************************************************/

#include "../Include/Logic.h"

namespace Pulse
{
	Logic::Logic( void )
		: m_pProcessManager( PSX_NULL ), m_state( EApplicationState::STARTUP )
	{

	}

	EErrorCode::Type Logic::Load( void )
	{
		return EErrorCode::NONE;
	}

	EErrorCode::Type Logic::Initialize( void )
	{
		m_pProcessManager = new ProcessManager;
		m_state = EApplicationState::STARTUP;

		if ( m_pProcessManager == PSX_NULL )
			return EErrorCode::LOGIC;

		return EErrorCode::NONE;
	}

	void Logic::Shutdown( void )
	{
		PSX_SafeDelete( m_pProcessManager );
		m_state = EApplicationState::STARTUP;

	}

	void Logic::OnUpdate( float time, float dt )
	{
		INT dtMilliSeconds = INT(dt * 1000.0f);

		switch( m_state )
		{
		case EApplicationState::STARTUP:
			// Do some pre initialization work
			ChangeState( EApplicationState::INITIALIZE );
			break;
		case EApplicationState::INITIALIZE:
			ChangeState( EApplicationState::RUNNING );
			break;
		case EApplicationState::RUNNING:
			m_pProcessManager->Update( dtMilliSeconds );
			break;
		case EApplicationState::SHUTDOWN:
			break;
		default:
			PSX_Assert( FALSE, "Undefined logic state." );
			break;
		};

		// TODO: Update views
	}

	void Logic::ChangeState( EApplicationState::Type state )
	{
		m_state = state;
	}

	Logic::~Logic( void )
	{
		Shutdown();
	}

}